package org.mspring.mlog.web.module.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.mspring.mlog.entity.Post;
import org.mspring.mlog.service.PostService;
import org.mspring.platform.web.freemarker.widget.stereotype.Widget;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

/**
 * 
 * @author Gao Youbo
 * @since 2013年7月18日
 */
@Widget
@RequestMapping("/blog")
public class Web_BlogWidget extends Web_AbstractWidget {
    @Autowired
    private PostService postService;

    @RequestMapping({ "", "/", "/list" })
    public String blog(HttpServletRequest request, HttpServletResponse response, Model model) {
        return "skin:/blog/index";
    }

    @RequestMapping("/post/{id}")
    public String post(@PathVariable Long id, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (id == null) {
            return "skin:/404";
        }
        Post post = postService.get(id);
        if (post != null && Post.Status.PUBLISH.equals(post.getStatus())) {
            return "redirect:/u/" + post.getAuthor().getName() + "/post?id=" + id;
        }
        return "skin:/404";
    }
}
